package com.ouyeel.cw.util;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.ouyeel.cw.model.Config;

import java.util.ArrayList;
import java.util.List;

public class Generator {

    public static void build(Config config) throws Exception {
//        String table="T_BIZ_CUSTOMER_ROLE_RES";
//        String entityName="BizCustomerRoleRes";
        String mapperName = config.getEntityName() + "Mapper";
        String author = "ttjkst";
//        String project="/home/ttjkst/ttjkst/Desktop/shgt-trade-trunk/shgt-trade/shgt-trade-service/";
//        String packagePath="com.shgt.trade.base.customer";

        //记得修改
        IdType idType = IdType.ASSIGN_ID;
//        Properties properties = new Properties();
//        properties.load(new FileInputStream(project+"/src/main/resources/application-dev.properties"));
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(config.getProjectPath() + "/src/main/java");
        gc.setOpen(false);
        gc.setAuthor(author);
        gc.setDateType(DateType.ONLY_DATE);
        gc.setEntityName(config.getEntityName());
        gc.setBaseResultMap(true);
        gc.setXmlName(mapperName);
        gc.setBaseColumnList(true);
        gc.setIdType(idType);
        gc.setFileOverride(true);
        gc.setMapperName(mapperName);

        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = config.getDataSourceConfig();
//        dsc.setUrl(properties.getProperty("spring.datasource.url"));
//        dsc.setSchemaName(properties.getProperty("spring.datasource.schema"));
//        dsc.setDriverName(properties.getProperty("spring.datasource.driver-class-name"));
//        dsc.setUsername(properties.getProperty("spring.datasource.username"));
//        dsc.setPassword(properties.getProperty("spring.datasource.password"));
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(null);
        pc.setParent(config.getPackageName());
        mpg.setPackageInfo(pc);


        TemplateConfig templateConfig = new TemplateConfig()
                .setEntity("templateVo/dto.java");
        if (!config.isCreateController()) {
            templateConfig.setController(null);
        }
        if (!config.isCreateService()) {
            templateConfig.setService(null);
            templateConfig.setServiceImpl(null);
        }
        if (!config.isCreateXml()) {
            templateConfig.setXml(null);
        }
        mpg.setTemplate(templateConfig);


        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setEntityBuilderModel(false);
        List<TableFill> tableFills = new ArrayList<>();
        strategy.setTableFillList(tableFills);
        strategy.setRestControllerStyle(true);
        strategy.setVersionFieldName("version");
        // 公共父类
        // 写于父类中的公共字段
        String[] ts = {config.getTableName()};
        strategy.setInclude(ts);
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new VelocityTemplateEngine());
        mpg.execute();
    }
}
